package com.rickpan.utils;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应工具类
 * 
 * @author RickPan Team
 * @since 2025-07-02
 */
public class ResponseUtils {

    /**
     * 成功响应
     * 
     * @param data 响应数据
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> success(Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("code", 200);
        response.put("message", "操作成功");
        response.put("data", data);
        return ResponseEntity.ok(response);
    }

    /**
     * 成功响应（无数据）
     * 
     * @param message 响应消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> success(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("code", 200);
        response.put("message", message);
        response.put("data", null);
        return ResponseEntity.ok(response);
    }

    /**
     * 成功响应（默认消息）
     * 
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> success() {
        return success("操作成功");
    }

    /**
     * 错误响应
     * 
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> error(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("code", 500);
        response.put("message", message);
        response.put("data", null);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
    }

    /**
     * 错误响应（自定义状态码）
     * 
     * @param code 状态码
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> error(int code, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("code", code);
        response.put("message", message);
        response.put("data", null);
        return ResponseEntity.status(code).body(response);
    }

    /**
     * 参数错误响应
     * 
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> badRequest(String message) {
        return error(400, message);
    }

    /**
     * 未授权响应
     * 
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> unauthorized(String message) {
        return error(401, message);
    }

    /**
     * 禁止访问响应
     * 
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> forbidden(String message) {
        return error(403, message);
    }

    /**
     * 资源不存在响应
     * 
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static ResponseEntity<Map<String, Object>> notFound(String message) {
        return error(404, message);
    }
}
